package com.swt.testdemo.util;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.io.IoUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author BleethNie
 * @version 1.0
 * @date 2020-09-25 9:12
 **/
public class AgyExcelUtil {


    public static Map<DateTime, ExcelReader> readerMap = new HashMap<>();
    public static Map<DateTime, Integer> readerLine = new HashMap<>();


    public static JSONObject readHeaderAndRow(DateTime current) {
        ExcelReader reader = readerMap.get(current);
        List<Object> header = reader.readRow(0);
        List<List<Object>> data = new ArrayList<>();
        int indexRow = 1;
        while (true) {
            List<Object> row = reader.readRow(indexRow);
            if (row == null || row.size() == 0) {
                break;
            }
            data.add(row);
            indexRow++;
            if (indexRow >= 51) {
                break;
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("header", header);
        jsonObject.set("data", data);
        reader.close();
        return jsonObject;
    }


    /**
     * 写出excel到本地
     *
     * @param file
     * @param headerList
     * @param rowList
     */
    public static void write(String file, List<String> headerList, List<List<String>> rowList) {
        ExcelWriter excelWriter = ExcelUtil.getWriter(true);
        File destFile = new File(file);
        excelWriter.setDestFile(destFile);

        excelWriter.setRowHeight(0, 20);
        excelWriter.writeHeadRow(headerList);


        if (rowList != null && rowList.size() != 0) {
            excelWriter.write(rowList);
        }
        excelWriter.close();
    }

    public static void httpWrite(HttpServletResponse response, String filename, List<String> headerList, List<List<String>> rowList) {
        ExcelWriter excelWriter = ExcelUtil.getWriter(true);


        excelWriter.setRowHeight(0, 20);
        excelWriter.writeHeadRow(headerList);


        if (rowList != null && rowList.size() != 0) {
            excelWriter.write(rowList);
        }

        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        excelWriter.flush(out, true);
        excelWriter.close();
        IoUtil.close(out);
    }

    public static void createExcelReader(ExcelReader reader, DateTime current) {
        readerMap.put(current, reader);
    }


    public static List<Map<String, String>> readRow(DateTime current, int pageSize) {
        ExcelReader reader = readerMap.get(current);
        List<Map<String, String>> rowList = new ArrayList<>();
        Integer line = readerLine.get(current);
        List<Object> header = reader.readRow(0);

        int index = 0;
        if (line != null) {
            index = line;
        }
        while (true) {
            index++;
            List<Object> list = reader.readRow(index);
            if (list == null || list.isEmpty()) {
                break;
            }
            Map<String, String> rowMap = new HashMap<>();
            for (int i = 0; i < list.size(); i++) {
                Object field = header.get(i);
                Object value = list.get(i);
                rowMap.put(Convert.toStr(field), Convert.toStr(value));
            }
            rowList.add(rowMap);
        }
        readerLine.put(current, index);
        return rowList;
    }

    public static void clearReader(DateTime current) {
        ExcelReader reader = readerMap.get(current);
        reader.close();
        readerMap.remove(current);
        readerLine.remove(current);
    }

}
